package de.listrong.da.ontology;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Resource;

import de.listrong.da.ontology.util.WriteToABox;
import de.listrong.da.tools.IdGenerator;
import de.listrong.da.tools.NewIDGener;
import gate.Annotation;

public class WordIndividual extends TextSegment implements
		Comparable<WordIndividual>, WriteToABox {

	/*--- private properties ---*/
	private String ontoClassName;
	private String type;
	private String content; 

	/*--- constructor ----*/
	public WordIndividual(Annotation annotation) {
		this.id = NewIDGener.newId(OntologyClasses.WORD);
                // IdGenerator.getInstance().newId(OntologyClasses.WORD);
		this.beginOffset = annotation.getStartNode().getOffset();
		this.endOffset = annotation.getEndNode().getOffset();
		this.ontoClassName = annotation.getFeatures().get("ontoClass")
				.toString();
		this.type = annotation.getFeatures().get("majorType").toString();
		this.content = annotation.getFeatures().get("content").toString();
	}

	/**
	 * 
	 * @param tbox
	 * @param abox
	 * @return a Individual of one Class in ontology, a object property or null.
	 */
	public Resource toDomainInstance(OntModel tbox, OntModel abox) {
		String tBoxDefaultNS = tbox.getNsPrefixURI("");
		String aBoxDefaultNS = abox.getNsPrefixURI("");
		
		OntClass ontClass = tbox.getOntClass(tBoxDefaultNS + this.ontoClassName);
		Individual domainInd = abox.createIndividual(aBoxDefaultNS + "IND-"
				+ String.valueOf(this.getId()), ontClass);
		
		DatatypeProperty content = tbox.getDatatypeProperty(tBoxDefaultNS + "content");
		domainInd.addLiteral(content, this.getContent());
		
		return domainInd;
	}

	/**
	 * create a Word Individual in A-Box from a Word. At same time an Individual as
	 * domain entity will also be created.
	 */
	@Override
	public Individual toOntIndiviual(OntModel tbox, OntModel abox) {
		String tBoxDefaultNS = tbox.getNsPrefixURI("");
		String aBoxDefaultNS = abox.getNsPrefixURI("");
		OntClass ontClass = tbox.getOntClass(tBoxDefaultNS + "Word");
		Individual wordInd = abox.createIndividual(aBoxDefaultNS + "WORD-"
				+ String.valueOf(this.getId()), ontClass);
		
		DatatypeProperty propBegin = tbox.getDatatypeProperty(tBoxDefaultNS	+ "beginAt");
		wordInd.addLiteral(propBegin, this.getBeginOffset());
		
		DatatypeProperty propEnd = tbox.getDatatypeProperty(tBoxDefaultNS + "endAt");
		wordInd.addLiteral(propEnd, this.getEndOffset());
		
		DatatypeProperty content = tbox.getDatatypeProperty(tBoxDefaultNS + "content");
		wordInd.addLiteral(content, this.getContent());
		
		ObjectProperty prop = tbox.getObjectProperty(tBoxDefaultNS + "depictsSLE");
		Individual domainInstance = (Individual)toDomainInstance(tbox, abox);
		wordInd.addProperty(prop, domainInstance);
		
		return wordInd;
	}

	/*--- Getter and Setter methods---*/
	public String getOntoClassName() {
		return ontoClassName;
	}

	public void setOntoClassName(String ontoClassName) {
		this.ontoClassName = ontoClassName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

    /* override methodes */
	@Override
	public int compareTo(WordIndividual o) {
		if (this.beginOffset < o.beginOffset) {
			return -1;
		} else if (this.beginOffset == o.beginOffset) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public String toString() {
		return "\tWord: id=" + this.getId() + ", beginOffset="
				+ this.beginOffset + ", endOffset=" + this.endOffset
				+ ", ontoClassName=" + this.ontoClassName
				+ ", type=" + this.type + ", content=" + this.content;
	}

}
